package com.pilipili.zeus.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduCourse;
import com.pilipili.zeus.eduService.entity.EduCourseDescription;
import com.pilipili.zeus.eduService.entity.excel.CourseExcelData;
import com.pilipili.zeus.eduService.entity.vo.*;
import com.pilipili.zeus.eduService.listener.CourseExcelListener;
import com.pilipili.zeus.eduService.mapper.EduCourseMapper;
import com.pilipili.zeus.eduService.service.EduChapterService;
import com.pilipili.zeus.eduService.service.EduCourseDescriptionService;
import com.pilipili.zeus.eduService.service.EduCourseService;
import com.pilipili.zeus.eduService.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 番剧 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;

    @Override
    public String saveCourseInfo(CourseInfo courseInfo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);

        boolean isSaveCourse = this.save(eduCourse);
        if (!isSaveCourse) {
            log.error("添加番剧失败");
            throw new MyException(20002,"添加番剧失败！");
        }
        //添加番剧简介表
        //番剧id
        String courseId = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfo.getDescription());
        eduCourseDescription.setId(courseId);
        boolean saveDescFlag = descriptionService.save(eduCourseDescription);
        if (!saveDescFlag) {
            log.error("添加番剧描述失败");
            throw new MyException(20002,"添加番剧描述失败！");
        }
        return courseId;
    }
    //获取番剧所有信息,转为描述对象,设置描述
    @Override
    public CourseInfo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(eduCourse,courseInfo);
        courseInfo.setDescription(courseDescription.getDescription());
        return courseInfo;
    }
    //修改番剧表,修改描述表
    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i==0){
            throw new MyException(20001,"修改信息失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo,description);
        descriptionService.updateById(description);
    }

    @Override
    public Publish getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public void pageQuery(Page<EduCourse> page, QueryCourse query) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("lesson_num");

        if (query == null){
            baseMapper.selectPage(page, queryWrapper);
            return;
        }

        String title = query.getTitle();
        String teacherId = query.getTeacherId();
        String subjectParentId = query.getSubjectParentId();
        String subjectId = query.getSubjectId();
        String begin = query.getBegin();
        String end = query.getEnd();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(subjectId) ) {
            queryWrapper.eq("subject_Id", subjectId);
        }
        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_Id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId) ) {
            queryWrapper.eq("subject_Parent_Id", subjectParentId);
        }

        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page, queryWrapper);

    }

    /**
     * 按顺序删除小节、章节、介绍、番剧
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {
        videoService.removeVideoByCourseId(courseId);
        chapterService.removeChapterByCourseId(courseId);
        descriptionService.removeById(courseId);
        int i = baseMapper.deleteById(courseId);
        if(i==0){
            throw  new MyException(20001,"删除失败");
        }
    }

    @Override
    public Map<String, Object> getCourseList(Page<EduCourse> page, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId()) ) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort()) ) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort()) ) {
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseQuery.getPriceSort()) ) {
            queryWrapper.orderByDesc("price");
        }
        queryWrapper.orderByDesc("buy_count");
        baseMapper.selectPage(page, queryWrapper);
        List<EduCourse> records = page.getRecords();

        Map<String, Object> map = new HashMap<>(20);

        map.put("items", records);
        map.put("current", page.getCurrent());
        map.put("pages",page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());

        return map;
    }

    @Override
    public CourseFront getChapterVideoById(String courseId) {
        return baseMapper.getCourseInfo(courseId);
    }

    @Override
    public void saveCourseDataByExcel(MultipartFile file, EduCourseService courseService) {
        try {
            //文件输入流,调用方法读取Excel
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, CourseExcelData.class,new CourseExcelListener(courseService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> getSortCourseInfo(IPage<SortCourse> page, String sortId) {
        IPage<SortCourse> courseIPage = baseMapper.getCourseInfoBySortId(page, sortId);
        List<SortCourse> courseList = courseIPage.getRecords();
        Map<String, Object> map = new HashMap<>(20);
        map.put("items", courseList);
        map.put("current", courseIPage.getCurrent());
        map.put("pages", courseIPage.getPages());
        map.put("size", courseIPage.getSize());
        map.put("total", courseIPage.getTotal());
        return map;
    }
}
